package com.office.stacks.pubilsh.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.office.web.controller.file.server.FileUploadServer;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.office.common.annotation.Log;
import com.office.common.core.controller.BaseController;
import com.office.common.core.domain.AjaxResult;
import com.office.common.enums.BusinessType;
import com.office.stacks.pubilsh.domain.Tasks;
import com.office.stacks.pubilsh.service.ITasksService;
import com.office.common.utils.poi.ExcelUtil;
import com.office.common.core.page.TableDataInfo;

/**
 * 任务发布Controller
 *
 * @author 柒月
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/publisher/task")
public class TasksController extends BaseController {
    @Autowired
    private ITasksService tasksService;

    @Autowired
    private FileUploadServer fileUploadServer;

    /**
     * 查询任务发布列表
     */
    @PreAuthorize("@ss.hasPermi('publisher:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tasks tasks) {
        startPage();
        List<Tasks> list = tasksService.selectTasksList(tasks);
        return getDataTable(list);
    }

    /**
     * 导出任务发布列表
     */
    @PreAuthorize("@ss.hasPermi('publisher:task:export')")
    @Log(title = "任务发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tasks tasks) {
        List<Tasks> list = tasksService.selectTasksList(tasks);
        ExcelUtil<Tasks> util = new ExcelUtil<Tasks>(Tasks.class);
        util.exportExcel(response, list, "任务发布数据");
    }

    /**
     * 获取任务发布详细信息
     */
    @PreAuthorize("@ss.hasPermi('publisher:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tasksService.selectTasksById(id));
    }


    /**
     * 新增任务发布
     */
    @PreAuthorize("@ss.hasPermi('publisher:task:add')")
    @Log(title = "任务发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestParam(required = false) Long[] userId, @RequestBody Tasks tasks) {
        return toAjax(tasksService.insertTasks(tasks, userId));
    }

    /**
     * 修改任务发布
     */
    @PreAuthorize("@ss.hasPermi('publisher:task:edit')")
    @Log(title = "任务发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tasks tasks) {
        return toAjax(tasksService.updateTasks(tasks));
    }

    /**
     * 修改任务表中的文件id为null
     *
     * @param id 文件id
     * @return 结果
     */
    @PutMapping("/{id}")
    public AjaxResult updateFileId(@PathVariable Long id) {
        return toAjax(tasksService.updateFileId(id));
    }


    /**
     * 删除任务发布
     */
    @PreAuthorize("@ss.hasPermi('publisher:task:remove')")
    @Log(title = "任务发布", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}/{fileId}")
    public AjaxResult remove(@PathVariable Long[] ids, @PathVariable Long[] fileId) {
        return toAjax(tasksService.deleteTasksByIds(ids, fileId));
    }
}
